using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Garfield.Runtime.Pico
{
    static class ReflectionExtension
    {
        public static bool Has<T>(this ICustomAttributeProvider parameter)
        {
            return parameter.IsDefined(typeof(T), false);
        }

        public static T[] Get<T>(this ICustomAttributeProvider parameter)
        {
            return parameter.GetCustomAttributes(typeof(T), false).Cast<T>().ToArray();
        }

        public static IEnumerable<PropertyInfo> GetProperties<T>(this Type target, BindingFlags flags)
        {
            return target.GetProperties(flags).Where(p => p.IsDefined(typeof (T), false));
        }

        public static IEnumerable<FieldInfo> GetFields<T>(this Type target, BindingFlags flags)
        {
            return target.GetFields(flags).Where(p => p.IsDefined(typeof(T), false));
        }

    }
}